package com.lishangshan.oas_system.controller.mail;

import com.github.pagehelper.util.StringUtil;
import com.lishangshan.oas_system.model.dao.maildao.InMailDao;
import com.lishangshan.oas_system.model.dao.maildao.MailnumberDao;
import com.lishangshan.oas_system.model.dao.maildao.MailreciverDao;
import com.lishangshan.oas_system.model.dao.system.StatusDao;
import com.lishangshan.oas_system.model.dao.system.TypeDao;
import com.lishangshan.oas_system.model.dao.user.UserDao;
import com.lishangshan.oas_system.model.entity.mail.Inmaillist;
import com.lishangshan.oas_system.model.entity.mail.Pagemail;
import com.lishangshan.oas_system.model.entity.system.SystemStatusList;
import com.lishangshan.oas_system.model.entity.system.SystemTypeList;
import com.lishangshan.oas_system.model.entity.user.User;
import com.lishangshan.oas_system.service.mail.MailServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
public class MailControllerFun {
    @Autowired
    private MailreciverDao mailreciverDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private InMailDao inMailDao;
    @Autowired
    private MailServices mailServices;
    @Autowired
    private StatusDao statusDao;
    @Autowired
    private TypeDao typeDao;
    @Autowired
    private MailnumberDao mailnumberDao;

    /**
     * 数据 封装了一波
     *
     * @param title
     * @param page
     * @param size
     * @param user
     * @return
     */
    public void paginfo(String title, int page, int size, User user, Model model, String val) {
        Page<Pagemail> pagelist = null;
        Page<Inmaillist> pagemail = null;
        List<Map<String, Object>> maillist = null;
    /*    if (("收件箱").equals(title) || ("垃圾箱").equals(title)) {
            //分页及查找
            pagelist = mailServices.recive(page, size, user, val, title);
            maillist = mailServices.mail(pagelist);
        } else if (("发件箱").equals(title) || ("草稿箱").equals(title)) {
            pagemail = mailServices.inmail(page, size, user, val, title);
            maillist = mailServices.maillist(pagemail);
        }*/
        if (("收件箱").equals(title)) {
            //分页及查找
            pagelist = mailServices.recive(page, size, user, val, title);
            maillist = mailServices.mail(pagelist);
        } else if (("发件箱").equals(title)) {
            pagemail = mailServices.inmail(page, size, user, val, title);
            maillist = mailServices.maillist(pagemail);
        } else if (("草稿箱").equals(title)) {
            pagemail = mailServices.inmail(page, size, user, val, title);
            maillist = mailServices.maillist(pagemail);
        } else {
            //垃圾箱
            //分页及查找
            val = "";
            pagelist = mailServices.recive(page, size, user, val, title);
            maillist = mailServices.mail(pagelist);

        }

        if (!Objects.isNull(pagelist)) {
            model.addAttribute("page", pagelist);
        } else {
            model.addAttribute("page", pagemail);

        }
  /*      map.put("pagelist", pagelist);
        map.put("pagemail", pagemail);
        map.put("maillist", maillist);*/
        System.out.println("maillist = " + maillist);
        model.addAttribute("maillist", maillist);
        model.addAttribute("url", "mailtitle");
        model.addAttribute("mess", title);
        model.addAttribute("sort", "&title=" + title + "&val=" + val);
//        return map;
    }

    /**
     * 获取 邮箱相关 下拉状态数据
     *
     * @param model
     * @param mail
     */
    public void typeInfo(Model model, Inmaillist mail) {
        if (mail == null) {
            List<SystemTypeList> typelist = typeDao.findByTypeModel("aoa_in_mail_list");
            List<SystemStatusList> statuslist = statusDao.findByStatusModel("aoa_in_mail_list");
            model.addAttribute("typelist", typelist);
            model.addAttribute("statuslist", statuslist);
        } else {
            model.addAttribute("status", statusDao.findOne(mail.getMailStatusid()));
            model.addAttribute("type", typeDao.findOne(mail.getMailType()));
        }
    }

    /**
     * 在 写信 编辑 回复 转发之间 封装不同的数据
     *
     * @param id
     * @param mail
     * @param huifu
     * @param model
     */
    public void redactInfo(Long id, Inmaillist mail, String huifu, Model model) {
        if (!StringUtil.isEmpty(id + "") && id != null) {
            //找到这条邮件
            mail = inMailDao.findOne(id);
            if (!StringUtil.isEmpty(huifu)) {
                model.addAttribute("title", huifu + mail.getMailTitle());
                model.addAttribute("content", mail.getContent());

            } else {
                model.addAttribute("title", mail.getMailTitle());
                model.addAttribute("content", mail.getContent());
            }
            typeInfo(model, mail);
            model.addAttribute("id", "回复");

        } else {
            model.addAttribute("id", "新发");
            typeInfo(model, mail);
        }
    }
}
